<?php
/**
 * 基于数组实现的栈
 */

namespace Xxb\XxbPhpAlgo;

use Xxb\XxbPhpAlgo\Contract\XxbStackInterface;

class ArrayStack implements XxbStackInterface
{
    private $stack;

    public function __construct($capacity = 10)
    {
        $this->stack = new FixedArray($capacity);
    }

    public function push($ele)
    {
        $this->stack->addLast($ele);
    }

    public function pop()
    {
        return $this->stack->removeLast();
    }

    public function bottom()
    {
        return $this->stack->getFirst();
    }

    public function top()
    {
        return $this->stack->getLast();
    }

    public function isEmpty()
    {
        return $this->stack->isEmpty();
    }

    public function getSize()
    {
        return $this->stack->getSize();
    }

    public function __toString()
    {
        $str = 'array stack: [';
        for ($i=0; $i<$this->getSize(); $i++) {
            $str .= $this->stack->get($i);
            $str .= ',';
        }
        $str = rtrim($str, ',');
        $str .= '] top';
        return $str;
    }
}
